'use strict';

/**
 * @description 企业/个人登录接口
 * @param {Object}  ctx 函数的执行上下文
 * @param {Object}  args 前端传递过来的参数
 */

const uniID = require('uni-id');
const { getNowFormatTime } = require('format-time');
const { SuccessModel, ErrorModel } = require('status-modal');

const db = uniCloud.database();
exports.main = async (args, ctx) => {
	const user = db.collection('user');
	const company = db.collection('company');
	const { type, account, password } = args;
	const collection = await (type == 'man' ? user : company).where({
		appId: account,
		appKey: password
	}).get();
	const arr = collection.data;
	if (arr.length) {
		let data = arr[0];
		if (!data.status) return new ErrorModel('管理员审核中')
		delete data.appId;
		delete data.appKey;
		let token = uniID.createToken({
			uid: data._id,
		}).token
		return new SuccessModel('登录成功', { ...data, token });
	} else return new ErrorModel('账号不存在');
};
